package com.ecoolex.ying.space.admin.graphql;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMember;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberManager;
import com.ecoolex.framework.modularize.api.service.audit.AuditRecord;
import com.ecoolex.framework.modularize.api.service.audit.AuditService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.admin.core.AdminConstant;
import com.ecoolex.ying.space.entity.UserWorks;
import com.ecoolex.ying.space.entity.UserWorksComment;
import com.ecoolex.ying.space.ws.UserWorksCommentService;
import com.ecoolex.ying.space.ws.UserWorksService;


@GraphQL2Module(type = Type.Admin, description = "用户作品、灵感评论管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY,
    key = UserWorksCommentGraphql.AUTH,
    name = "用户作品、灵感评论管理")
public class UserWorksCommentGraphql {

    public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-USERWORKSCOMMENT";

    @Autowired
    private AuditService auditService;

    @Reference
    private UserWorksCommentService userWorksCommentService;

    @Reference
    private UserWorksService userWorksService;

    @Autowired
    private ConsumerMemberManager consumerMemberManager;

    @TypeField(type = UserWorksComment.class, field = "auditRecord", description = "审核记录")
    public AuditRecord auditRecord(@Source UserWorksComment source) {
        return auditService.record(source.getAuditRecordId());
    }

    @TypeField(type = UserWorksComment.class, field = "commentUser", description = "评论的用户")
    public ConsumerMember getCommentUser(@Source UserWorksComment source) {
        return consumerMemberManager.getByUserId(source.getCommentUserId());
    }

    @TypeField(type = UserWorksComment.class, field = "beReplyUser", description = "被回复用户")
    public ConsumerMember getBeCommentUser(@Source UserWorksComment source) {
        return consumerMemberManager.getByUserId(source.getBeCommentUserId());
    }

    @TypeField(type = UserWorksComment.class, field = "userWorks", description = "用户作品、灵感")
    public UserWorks userWorks(@Source UserWorksComment source) {
        return userWorksService.getUserWorksById(source.getWorksId());
    }

    private static final String AUTH_GET = AUTH + "-GET";

    @QueryField(field = "userWorksComment", description = "评论详情")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "评论详情")
    public UserWorksComment getById(@NonNull Integer commentId, AdminMemberAuthority user) {
        user.assertLogin();
        return userWorksCommentService.getById(commentId);
    }

    private static final String AUTH_OPUS_QUERY = AUTH + "-QUERYOPUS";

    @QueryField(field = "userWorksOpusCommentList", description = "作品评论列表")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_OPUS_QUERY, name = "作品评论列表")
    public Pagination<UserWorksComment> queryOpus(String title, @NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
        user.assertLogin();
        return userWorksCommentService.queryByOpus(title, sorts, page);
    }

    private static final String AUTH_OPUS_INSPIRATION = AUTH + "-QUERYINSPIRATION";

    @QueryField(field = "userWorksInspirationCommentList", description = "灵感评论列表")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_OPUS_INSPIRATION, name = "灵感评论列表")
    public Pagination<UserWorksComment> queryInspiration(String title, @NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
        user.assertLogin();
        return userWorksCommentService.queryByInspiration(title, sorts, page);
    }

    private static final String AUTH_DEL = AUTH + "-DEL";

    @MutationField(field = "delUserWorksComment", description = "删除评论")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除评论")
    public boolean delUserWorksComment(@NonNull Integer commentId, AdminMemberAuthority user) {
        user.assertAuth(AUTH_DEL);
        UserWorksComment userWorksComment = userWorksCommentService.getById(commentId);
        Assert.isTrue(Check.notNull(userWorksComment), ResultCode.VERIFY.build(1), "评论不存在");
        userWorksCommentService.delete(userWorksComment, user.userName());
        return true;
    }
}
